package madixin.t1109;

class Solution {

    //思路：利用差分法，参考https://leetcode-cn.com/problems/corporate-flight-bookings/solution/ling-huo-li-yong-chai-fen-fa-jie-jue-gai-ti-by-xsl/
    public int[] corpFlightBookings(int[][] bookings, int n) {
//        输入：bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
//        输出：[10,55,45,25,25]
        int res[] = new int[n];
        for (int[] booking : bookings) {
            res[booking[0] - 1] += booking[2];

            if (booking[1] < n) {
                res[booking[1]] -= booking[2];
            }
        }
        for (int i = 1; i < n; i++) {
            res[i] += res[i - 1];
        }
        return res;
    }

    //暴力法添加
    public int[] corpFlightBookings2(int[][] bookings, int n) {
//        输入：bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
//        输出：[10,55,45,25,25]
        int res[] = new int[n];
        for (int[] booking : bookings) {
            for (int i = booking[0]; i <= booking[1]; i++) {
                res[i - 1] += booking[2];
            }
        }
        return res;
    }
}